package web.reclamos.dao.sql;

//Patron Singleton


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import web.reclamos.util.Parametros;

public class ConexionDB {

    private static ConexionDB instance;

    private ConexionDB() {

    }

    public static ConexionDB getInstance() {
        if(instance == null)
                instance = new ConexionDB();

        return instance;
    }

    public Connection getConnection() throws SqlReclamosException {
         Connection cn = null;
        Parametros p = Parametros.getInstance();
        try {
            //Registro de eventos (sucesos)
            //Logging
            //Class.forName(p.getProperty("driver"));
            Class.forName("com.mysql.jdbc.Driver");

            //String url = p.getProperty("url");
            String url = "jdbc:mysql://localhost:3306/reclamos";
            //Data Source=(local);Initial Catalog=ssggcc;Persist Security Info=True;User ID=sa
            //jdbc:sqlserver://localhost;databaseName=reclamos;integratedSecurity=true;

            //Logging
            //cn = DriverManager.getConnection(url, p.getProperty("user"),p.getProperty("pwd"));
            cn = DriverManager.getConnection(url, "root", "");

        }catch(ClassNotFoundException ex){
            //Registrar errores (Logging)
            System.out.println(ex);
            throw new SqlReclamosException("No se pudo cargar el Driver");
        }catch(SQLException ex){
            System.out.println(ex);
            throw new SqlReclamosException("Error de conexion a la BD");
        }catch(Exception ex) {
            System.out.println(ex);
            throw new SqlReclamosException("Error desconcido");
        }

        return cn;
    }

    public void close(Connection cn){
        try {
            if(cn != null)
                cn.close();
        }catch(SQLException ex){
             //Registrar errores (Logging)
            System.out.println(ex);
        }
    }
    public void close(Statement stm){
   try {
            if(stm != null)
                stm.close();
        }catch(SQLException ex){
             //Registrar errores (Logging)
            System.out.println(ex);
        }
    }
    public void close(ResultSet rs){
   try {
            if(rs != null)
                rs.close();
        }catch(SQLException ex){
             //Registrar errores (Logging)
            System.out.println(ex);
        }
    }
}
